package cn.yuan.pet.service.impl;

import cn.yuan.basic.service.impl.BaseServiceImpl;
import cn.yuan.basic.util.CodeGenerateUtils;
import cn.yuan.basic.util.DistanceUtil;
import cn.yuan.basic.util.PageList;
import cn.yuan.basic.util.Point;
import cn.yuan.order.domain.PetAcquisitionOrder;
import cn.yuan.order.mapper.PetAcquisitionOrderMapper;
import cn.yuan.org.domain.Employee;
import cn.yuan.org.domain.Shop;
import cn.yuan.org.mapper.EmployeeMapper;
import cn.yuan.org.mapper.ShopMapper;
import cn.yuan.pet.domain.Pet;
import cn.yuan.pet.domain.SearchMasterMsg;
import cn.yuan.pet.mapper.SearchMasterMsgMapper;
import cn.yuan.pet.query.SearchMasterMsgQuery;
import cn.yuan.pet.service.IPetService;
import cn.yuan.pet.service.ISearchMasterMsgService;
import cn.yuan.user.domain.User;
import cn.yuan.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginInfoId) {
        //1.寻找最近店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //计算最近的店铺
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        //2.绑定最近店铺
        searchMasterMsg.setShop_id(nearestShop.getId());
        //3.绑定用户信息
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        //4.存储寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
        //5.给店铺老板发个消息
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        /*SmsUtils.send(employee.getPhone(), "老板您好，来【"+searchMasterMsg.getTitle()+"】活了，赶快去接客!");*/
        System.out.println("老板您好，来【"+searchMasterMsg.getTitle()+"】活了，赶快去接客!");
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(0);
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        if(employee.getShop_id()!=null){//属于店铺的消息
            query.setShopId(employee.getShop_id());
        }
        return super.queryForPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(1);
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        if(employee.getShop_id()!=null){//属于店铺的消息
            query.setShopId(employee.getShop_id());
        }
        return super.queryForPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> myMsg(SearchMasterMsgQuery query, Long loginInfoId) {
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        query.setUserId(user.getId());
        return super.queryForPage(query);
    }

    @Override
    public void handle(Pet pet,Long logininfoId) {

        Long searchMasterMsgId = pet.getSearch_master_msg_id();
        //1.调整信息状态
        searchMasterMsgMapper.updateState4Proceed(searchMasterMsgId);
        //2.保存宠物信息  返回自增id
        //3.保存宠物详情 设置宠物id
        petService.save(pet);
        //3创建订单
        Employee employee = employeeMapper.loadByLoginInfoId(logininfoId);

        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(searchMasterMsgId);

        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet,searchMasterMsg,employee.getId());

        petAcquisitionOrderMapper.save(petAcquisitionOrder);
        //4 @TODO 如果要收钱，创建支付单并付完款
        if(petAcquisitionOrder.getPrice().longValue()>0){
            System.out.println("你已经付款了。。。。。");
        }
    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg searchMasterMsg, Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(searchMasterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(searchMasterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }


//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
